package com.xyzwps.collections

import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.*

class TestRange {

    @Test
    void 'test create(sup)'() {
        for (int i = -1; i > -234; i--) {
            assertThrows IllegalArgumentException.class, { Range.create(i) }
        }
        for (int i = 0; i < 20; i++) {
            int sum = 0
            for (int v : Range.create(i)) {
                sum += v
            }
            int target = ((i + 1) * i / 2) as int
            assertEquals sum, target
        }
    }

    @Test
    void 'test create(inf, sup)'() {
        for (int inf = 0; inf < 32; inf++) {
            for (int sup = 0; sup < 43; sup++) {
                if (sup < inf) {
                    assertThrows IllegalArgumentException.class, { Range.create(inf, sup) }
                } else if (sup == inf) {
                    for (int v : Range.create(inf, sup))
                        assertEquals v, inf
                } else {
                    int sum = 0
                    for (int v : Range.create(inf, sup)) {
                        sum += v
                    }
                    int target = ((inf + sup) * (sup - inf + 1) / 2) as int
                    assertEquals sum, target
                }
            } // end sup for
        } // end inf for
    }
}
